#include "Netdefs.h"
#include "Parsing.h"

#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

#include <fstream>
using std::ifstream;
using std::ofstream;

extern "C" __declspec(dllexport) void SendCarMessage()
{
	SocketClient *mysock;

	string ip, car;
	ifstream ifs("input.txt");
	ifs >> ip >> car;
	
	/*string ip, car;
	string str(param);

	unsigned i;
	for (i = 0;str[i] != ' ';++i);
	
	ip = str.substr(0,i);
	car = str.substr(i+1, str.size() - 1);*/
	
	try {
		mysock = new SocketClient(ip, SERVERPORT);
	}
	catch (...) {
		cout << "Couldn't connect to " << ip << endl;
		return;
	}
	unsigned long car_lp(Auxiliary::fromString<unsigned long>(car));
	Protocol::ui2db_carRequest(*mysock, car_lp);
	string rcv(mysock->ReceiveLine());
	for (int i=0; i<6; i++)
		rcv += "\n" + mysock->ReceiveLine();
	rcv += "\n";

	ofstream ofs("output.txt");
	ofs << rcv;
	
	delete mysock;
	return;
}

extern "C" __declspec(dllexport) void SendPLotMessage()
{

	string ip;
	ifstream ifs("input.txt");
	ifs >> ip;

	SocketClient *mysock;
	try {
		mysock = new SocketClient(ip, SERVERPORT);
	}
	catch (...) {
		cout << "Couldn't connect to " << ip << endl;
		return;
	}
	
	Protocol::ui2db_plotRequest(*mysock);
	string rcv(mysock->ReceiveLine());

	for (int i=0; i<4; i++)
		rcv += "\n" + mysock->ReceiveLine();
		
	rcv += "\n";

	ofstream ofs("output.txt");
	ofs << rcv;
	
	delete mysock;
	return;
}